/*
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      MemoryInvIndexStorage.hpp
 *
 * $FTS$
 */
#ifndef _MEMORY_STORAGE_HPP__
#define _MEMORY_STORAGE_HPP__ 1

/**
  @file MemoryInvIndexStorage.hpp
  @brief Memory storage
*/

// Local Includes
#include <InvIndexStorage.hpp>
#include <DataDumper.hpp>
#include <Hash.hpp>
#include <HashFunctions.hpp>

#define C_INV_IDX_READABLE "Main inverted index memory data dump (uncompressed)"
#define C_INV_IDX_TYPE     0x00000002

namespace FTSS // FullTextSearchSystem
{

/**
  @class MemoryInvIndexStorage MemoryInvIndexStorage.hpp <MemoryInvIndexStorage.hpp>
  @brief Memory data storage
*/
class MemoryInvIndexStorage:
  public InvIndexStorage
{
public:
	/**
	  @brief Constructor
	  @param iDictSize - Initial dictionary size
	*/
	MemoryInvIndexStorage(Size_t iDictSize = (1 << 0));

	/**
	  @brief Serialize and dump object
	  @return 0 success, -1 - if any error occured
	*/
	INT_32 Dump(DataDumper * pObject);

	/**
	  @brief Restore and de-serialize object
	  @return 0 success, -1 - if any error occured
	*/
	INT_32 Restore(DataDumper * pObject);

	/** A destructor */
	~MemoryInvIndexStorage() throw();

private:

	/**
	  @struct StorageValue MemoryInvIndexStorage.hpp <MemoryInvIndexStorage.hpp>
	  @brief Raw data wrapper
	*/
	struct StorageValue
	{
		/** Raw data        */
		void * data;
		/** Raw data length */
		Size_t data_length;
		/** Constructor     */
		inline StorageValue(): data(NULL), data_length(0) { ;; }
	};

	/**
	  @typedef Pair<TokenHash_t, StorageValue> StoragePair
	  @brief Data storage key => value pair type definition
	*/
	typedef Pair<TokenHash_t, StorageValue> StoragePair;

	/**
	  @typedef Hash<TokenHash_t, StorageValue, HashHasher, HashComparator> StorageHash
	  @brief Data storage type definition
	*/
	typedef Hash<TokenHash_t, StorageValue, HashHasher, HashComparator> StorageHash;

	/** Data storage                      */
	StorageHash oStorageHash;
	/** Number of documents in collection */
	Size_t      iDocNum;
	/** Zones in collection               */
	Zone      * aZones;
	/** Number of zones in collection     */
	Size_t      iZonesCount;

	// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Private methods
	//

	/**
	  @brief Store document entry in specified data storage
	  @param iWordId - Word ID (word hash)
	  @param pEntry - Document Entry
	  @param iDocEntrySize - Document Entry size
	*/
	void StoreDocumentEntry(WordId_t iWordId, DocumentEntry * pEntry, const Size_t & iDocEntrySize);

	/**
	  @brief Store number of documents in collection
	  @param iIDocNum - Number of documents
	*/
	void StoreDocNum(const Size_t & iIDocNum);

	/**
	  @brief Get document entry from specified data storage
	  @param iWordId - Word ID (word hash)
	  @return pair of iterators
	*/
	Pair<DocumentEntryIterator, DocumentEntryIterator> GetDocumentEntry(const WordId_t & iWordId);

	/**
	  @brief Get number of documents in collection
	  @return Number of documents
	*/
	Size_t GetDocNum();

	/**
	  @brief Get document frequency for specified token
	  @param iWordId - Word ID (word hash)
	  @return document frequency (number of documents, contains whis word).
	*/
	Size_t GetDF(const WordId_t & iWordId);

	/**
	  @brief Check deletion flag of document
	*/
	INT_32 IsDeleted(const Size_t & iDocNum);

	/**
	  @brief Get list of all available zones in this document collection
	  @param iZonesCount - count of zones
	  @return List of zones
	*/
	Zone * GetZones(INT_32 & iZonesCount);

	/**
	  @brief Get list of all available zones in this document collection
	  @param aZones - list of zones
	  @param iZonesCount - number of zones
	*/
	void StoreZones(const Zone * aZones, const INT_32 & iZonesCount);
};

} // namespace FTSS
#endif // _MEMORY_STORAGE_HPP__
// End.
